#!/bin/bash
#

check_package(){
	name=$1
	if rpm -q $name &> /dev/null;then
		echo "package installed"
	else
		echo "package not installed"
		yum install -y $name
		systemctl restart $name
	fi
}

check_port(){
	read -p "检查是否端口占用，输入服务端口：" port
	count=$(netstat -anplt | grep :80 | wc -l)
	if [ $count -eq 0 ];then
		echo "没有服务占用端口"
	else
		echo "有服务占用端口：$port"
		zname=$(netstat -anplt | grep :80 | awk -F'/' '{print $2}')
		echo "占用端口服务：$zname"
	fi
}

check_firewalld(){
	systemctl status firewalld &> /dev/null
	result=$?
	if [ $result -eq 0 ];then
		echo "防火墙启动中，关闭防火墙"
		systemctl stop firewalld
		systemctl disable firewalld
	else
		echo "防火墙关闭中"
	fi
}



read -p "输入服务名：" name

systemctl restart $name &> /dev/null
result=$?
if [ $result -eq 0 ];then
   echo "服务${name}正在运行"
else
   check_package $name
   check_port
   check_firewalld
fi   
